package org.example.pims.handler;

import org.example.commons.constant.HttpStatusConstant;
import org.example.commons.exception.CustomException;
import org.example.commons.vo.ResultVO;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * Created by bothin
 * At 2019/12/22 9:35
 * Description:
 */
@RestControllerAdvice
public class HandlerControllerException {

    @ExceptionHandler(Exception.class)
    public ResultVO handleException(Exception e) {
        if (e instanceof AccessDeniedException) {
            return ResultVO.error(HttpStatusConstant.FORBIDDEN, "你的访问权限不足！");
        } else if (e instanceof CustomException) {
            Integer code = ((CustomException) e).getCode();
            if (code != null && code == HttpStatusConstant.UNAUTHORIZED)
                return ResultVO.error(HttpStatusConstant.FORBIDDEN, "你尚未登录授权");
            else
                return ResultVO.error(HttpStatusConstant.ERROR,e.getMessage());
        }
        return ResultVO.error("哎哟系统断片啦！" + e.getMessage());
    }
}
